package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.exception.ServiceException;
import com.qs.model.entity.Address;
import com.qs.mapper.AddressMapper;
import com.qs.response.ResponseEnum;
import com.qs.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户地址表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-02
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Override
    public void saveAddress(Address address) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", address.getUserId());
        List<Address> addresses = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(addresses)){
            //默认地址只能有一个:状态为1表示设置为默认地址
            if(1 == address.getStatus()){
                List<Address> collect = addresses.stream().filter(item -> item.getStatus() == 1).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(collect)){
                    throw new ServiceException(ResponseEnum.ADDRESS_ERROR);
                }
            }else{
                baseMapper.insert(address);
            }
        }else{
            baseMapper.insert(address);
        }
    }

    @Override
    public List<Address> queryAddress(Integer userId) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc("status").orderByDesc("create_time");
        List<Address> addresses = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(addresses)){
            return addresses;
        }
        return null;
    }

    @Override
    public void updateAddress(Address address) {
        Address oldAddress = baseMapper.selectById(address.getId());
        if(oldAddress == null){
            throw new ServiceException(ResponseEnum.ADDRESS_NOT_FOUND);
        }
        //如果修改的地址不是默认地址即可直接修改
        if(1 != address.getStatus()){
            baseMapper.updateById(address);
            return;
        }
        //说明现在修改的地址是想设置为默认地址
        List<Address> addressList = this.queryAddress(address.getUserId());
        if(!CollectionUtils.isEmpty(addressList)){
            //不包含当前要修改的地址数据
            List<Address> filterList = addressList.stream().filter(item -> item.getId().intValue() != address.getId()).collect(Collectors.toList());

            if(!CollectionUtils.isEmpty(filterList)){
                List<Address> list = filterList.stream().filter(item -> item.getStatus().intValue() == 1).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(list)){
                    throw new ServiceException(ResponseEnum.ADDRESS_ERROR);
                }
                baseMapper.updateById(address);
            }
        }
    }
}
